package com.bd.scm.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bd.scm.domain.dto.login.SysLogDto;
import com.bd.scm.module.jurisdiction.service.BdSysRoleService;
import com.bd.scm.module.log.service.BdSysLogService;
import com.bd.scm.module.utils.DateUtil;
import com.bd.utils.DateUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * @author YH
 * @version 1.0
 * @date 2020/12/15 17:17
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private BdSysLogService bdSysLogService;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(com.bd.scm.config.MyLog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
//        try {
//            System.out.println("切面。。。。。");
//            //保存日志
//            SysLogDto sysLog = new SysLogDto();
//
//            //从切面织入点处通过反射机制获取织入点处的方法
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            //获取切入点所在的方法
//            Method method = signature.getMethod();
//
//            //获取操作
//            MyLog myLog = method.getAnnotation(MyLog.class);
//            if (myLog != null) {
//                String value = myLog.value();
//                sysLog.setOperation(value);//保存获取的操作
//            }
//
//            //获取请求的类名
//            String className = joinPoint.getTarget().getClass().getName();
//            //获取请求的方法名
//            String methodName = method.getName();
//            sysLog.setMethod(className + "." + methodName);
//
//            //请求的参数
//            Object[] args = joinPoint.getArgs();
//            if(!"停车场远程管理-车辆权限记录导入".equals(sysLog.getOperation())){
//                String params = JSON.toJSONString(args[0]);
//                sysLog.setParams(params);
//            }else{
//                sysLog.setParams("文件流无法转换参数");
//            }
//
//            sysLog.setCreateDate(DateUtils.getCurrentDateTime());
//            //获取session中当前用户
//            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//            HttpSession session = request.getSession();
//            JSONObject user = (JSONObject)session.getAttribute("user");
//            //获取用户名
//            sysLog.setUsername(user.getString("userName")+"("+user.getString("userSerial")+")");
//            //获取用户ip地址
////        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//            sysLog.setIp(InetAddress.getLocalHost().getHostAddress());
//
//            //调用service保存SysLog实体类到数据库
//            this.bdSysLogService.insertBdSysLog(sysLog);
//            System.out.println("<<<================== "+sysLog.toString()+" ==================>>>");
//            System.out.println("将日志记录到数据库");
//        } catch (Exception exception){
//            exception.printStackTrace();
//            System.out.println("<<<================ 系统日志保存失败:"+exception.getMessage()+" ================>>>");
//        }
    }
}
